﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using System.Data;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class MngStandard : LDSP.BRL.BaseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitDataDynamicBefore();
                if (!IsPostBack)
                {
                    this.BindTree();
                    this.InitTreeNodeSelection();
                    this.InitData();
                    this.BindGrid();
                }
                this.InitDataDynamicAfter();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected Catalog m_catalog = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_catalog = null;
            }
        }
        protected Catalog Catalog
        {
            get
            {
                if (m_catalog == null)
                {
                    m_catalog = new Catalog();
                    if (LastID != 0)
                    {
                        m_catalog.GetProperties(LastID);
                    }
                }
                return m_catalog;
            }
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "name";
            g_pageInfo.PageSize = 10;
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }

        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.GetCondition();
            this.SetSpecialPageInfo();
            this.BindDDLIsValid();
            this.BindDDLPriority();
            this.JSShowModalDialog("cid=" + LastID, this.btn_addNew);
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void InitDataDynamicAfter()
        {
            this.JSSetDateTimeReadOnly(this.txt_releaseStartDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_releaseEndDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_implementStartDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_implementEndDT.ClientID);
        }
        protected void BindGrid()
        {
            
            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = BYFW.SFL.CommonOP.SortedTable(new Codification().GetRecords(g_pageInfo.ConditionWhere, true), g_pageInfo.GetSortExpression());
            this.grd_list.DataBind();
        }
        protected void BindTree()
        {
            this.tre_obj.Nodes.Clear();
            List<Catalog> rootCatalogs = new Catalog().GetRootListByDefault();
            for (int i = 0; i < rootCatalogs.Count; i++)
            {
                TreeNode nd = new TreeNode();
                nd.Text = rootCatalogs[i].Name;
                nd.Value = rootCatalogs[i].ID.ToString();
                this.tre_obj.Nodes.Add(nd);
                this.BindTreeAssis(nd, rootCatalogs[i].ID);
            }
    
        }
        protected void BindTreeAssis(TreeNode parent, int parentID)
        {
            List<Catalog> catalogs = new Catalog().GetListByParentID(parentID);
            for (int i = 0; i < catalogs.Count; i++)
            {
                TreeNode nd = new TreeNode();
                nd.Text = catalogs[i].Name;
                nd.Value = catalogs[i].ID.ToString();
                parent.ChildNodes.Add(nd);
                this.BindTreeAssis(nd, catalogs[i].ID);
            }
        }
        protected void InitTreeNodeSelection()
        {
            if (this.tre_obj.Nodes.Count > 0)
            {
                this.tre_obj.Nodes[0].Select();
                this.LastID = this.GetInt(this.tre_obj.SelectedValue);
            }
        }

        protected void BindDDLPriority()
        {
            this.ddl_priority.Items.Clear();
            this.ddl_priority.Items.Add(new ListItem("--请选择--", "-1"));
            this.ddl_priority.Items.Add(new ListItem(Priority.低.ToString(), ((int)Priority.低).ToString()));
            this.ddl_priority.Items.Add(new ListItem(Priority.中.ToString(), ((int)Priority.中).ToString()));
            this.ddl_priority.Items.Add(new ListItem(Priority.高.ToString(), ((int)Priority.高).ToString()));
            try
            {
                this.ddl_priority.SelectedIndex = 0;
            }
            catch { }

        }
        protected void BindDDLIsValid()
        {
            this.ddl_isValid.Items.Clear();
            this.ddl_isValid.Items.Add("--请选择--");
            this.ddl_isValid.Items.Add(new ListItem("无效", "0"));
            this.ddl_isValid.Items.Add(new ListItem("有效", "1"));
            try
            {
                this.ddl_priority.SelectedIndex = 0;
            }
            catch { }

        }

        protected void GetCondition()
        {
            string cond = DefaultDB.GetSQLNormalCondition("catalogID", EDataType.Int, LastID.ToString(), "=", true);
            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("enterpriseID", EDataType.Int, "0", "=", true), true);
            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("docType", EDataType.Int, ((int)DocType.标准).ToString(), "=", true), true);
            if (this.txt_name.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLMultiCondition("name", EDataType.String, this.txt_name.Text.Trim(), true, true), true);
            }
            if (this.txt_referenceNumber.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLMultiCondition("referenceNumber", EDataType.String, this.txt_referenceNumber.Text.Trim(), true, true), true);
            }
            if (this.ddl_priority.SelectedIndex > 0)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("priority", EDataType.Int, this.ddl_priority.SelectedValue,"=", true), true);
            }
            if (this.ddl_isValid.SelectedIndex > 0)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("isValid", EDataType.Bool, this.ddl_isValid.SelectedValue, "=", true), true);
            }
            if (this.txt_releaseStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("releaseDT", EDataType.Date, this.txt_releaseStartDT.Text, ">=", true), true);
            }
            if (this.txt_releaseEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("releaseDT", EDataType.Date, this.txt_releaseEndDT.Text, "<=", true), true);
            }
            if (this.txt_implementStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("implementDT", EDataType.Date, this.txt_implementStartDT.Text, ">=", true), true);
            }
            if (this.txt_implementEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("implementDT", EDataType.Date, this.txt_implementEndDT.Text, "<=", true), true);
            }
            g_pageInfo.Condition = cond;
        }

        private void JSShowModalDialog(string arg, WebControl ctrl)
        {
            this.JSShowDialog("AddStandard.aspx?" + arg, 600, 600, ctrl, null, null, null, null, null, null, null, true, null, null, null, null, null);
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                Codification ett = new Codification();
                ett.GetProperties(id);
                if (e.CommandName == "downloadItem")
                {
                    this.DownloadDemo(id);//todo remove
                    this.StartDownload(ett.FileName, ett.GetDownloadFileFullName());
                }
                else if (e.CommandName == "deleteItem")
                {
                    ett.Delete();
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Codification ett = new Codification();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    e.Row.Cells[1].Text = (this.grd_list.PageIndex * this.grd_list.PageSize + e.Row.RowIndex + 1).ToString();
                    WebControl c = (WebControl)e.Row.FindControl("LinkButton2");
                    this.JSShowModalDialog("id=" + ett.ID, c);
                    e.Row.Cells[3].Text = ett.Priority.ToString();
                    e.Row.Cells[4].Text = ett.Category.Name;
                    e.Row.Cells[7].Text = ett.ReleaseDT.ToString("yyyy-MM-dd");
                    e.Row.Cells[8].Text = ett.ImplementDT.ToString("yyyy-MM-dd");
                    e.Row.Cells[9].Text = ett.IsValid ? "是" : "否";
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void tre_obj_SelectedNodeChanged(object sender, EventArgs e)
        {
            LastID = this.GetInt(this.tre_obj.SelectedValue);
            this.GetCondition();
            this.BindGrid();
            this.JSShowModalDialog("cid=" + LastID, this.btn_addNew);
        }

        protected void DownloadDemo(int id)
        {
            Enterprise et = new Enterprise();
            List<Enterprise> etList = et.GetSpecialList<Enterprise>(null, "");
            int index = new Random().Next(etList.Count);
            int etID = etList[index].ID;
            new DownloadLog().CreateNew(etID, id);
        }
    }
}